1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2014 Devisualization (Richard Andrew Cattermole)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 module devisualization.util.opengl.shaders.basictexture;
25 import devisualization.util.opengl.shaders.defs;
26 import devisualization.util.opengl.vertexarray;
27 import devisualization.util.opengl.buffers;
28 import gl3n.linalg : vec4, mat4;
29 deprecated("de_util:opengl is going to die"):
30 
31 /**
32  * Renders vertices with a texture
33  * 
34  * Shader based upon https://github.com/fogleman/pg/blob/master/pg/programs.py
35  */
36 class BasicTextureShader : ShaderProgram {
37 	this() {
38 		super("""
39 #if __VERSION__ > 120
40     #version 130
41     in vec4 position;
42     in vec2 uv;
43     out vec2 frag_uv;
44 #else
45     #version 120
46     attribute vec4 position;
47     attribute vec2 uv;
48     varying vec2 frag_uv;
49 #endif
50 
51 uniform vec4 move;
52 uniform mat4 scale;
53 uniform mat4 transform;
54 
55 void main() {
56     gl_Position = ((scale * position) + move) * transform;
57     frag_uv = uv;
58 }
59 			""",
60 			"""
61 #if __VERSION__ > 120
62     #version 130
63     uniform sampler2D sampler;
64     uniform vec4 color;
65 
66     in vec2 frag_uv;
67     out vec4 outColor;
68 	void main() {
69 	    vec4 color = texture2D(sampler, frag_uv);
70 	    if (color.a == 0) {
71 	        discard;
72 	    }
73 	    outColor = color;
74 	}
75 #else
76     #version 120
77     uniform sampler2D sampler;
78     uniform vec4 color;
79 
80     varying vec2 frag_uv;
81 	void main() {
82 	    vec4 color = texture2D(sampler, frag_uv);
83 	    if (color.a == 0) {
84 	        discard;
85 	    }
86 	    gl_FragColor = color;
87 	}
88 #endif
89 		""");
90 	}
91 
92 	@disable {
93 		this(string vert, string frag=null, string geom=null){}
94 		this(Shader vert = null, Shader frag = null, Shader geom = null){}
95 	}
96 
97 	@property {
98 		void move(vec4 v) {
99 			uniform("move", v);
100 		}
101 		
102 		void scale(mat4 m) {
103 			uniform("scale", m);
104 		}
105 		
106 		void transform(mat4 m) {
107 			uniform("transform", m);
108 		}
109 
110 		void myVertices(VertexArray vao, IBuffer buffer) {
111 			import devisualization.util.opengl.function_wrappers.v20 : AttribPointerType;
112 			vao.bindAttribute(this, "position", buffer, AttribPointerType.Float, 4);
113 		}
114 
115 		void myTextureCoordinates(VertexArray vao, IBuffer buffer) {
116 			import devisualization.util.opengl.function_wrappers.v20 : AttribPointerType;
117 			vao.bindAttribute(this, "uv", buffer, AttribPointerType.Float, 2);
118 		}
119 	}
120 }